<template>
	<!-- 商家端 代金券 -->
	<view class="">
		<view class="list" v-if="list.length>0">
			<view class="list-item" v-for="(item,index) in list" :key="index">
				<view class="list-item-left">
					<image class="lil-avatar" :src="url+item.goods.detail" mode=""></image>
					<view class="">
						<view class="lil-price">
							下单送{{item.gift_number}} <text>张</text>
						</view>
						<view class="lil-name">
							{{item.goods.name}}
						</view>
					</view>
				</view>
				<view class="list-item-right">
					<button class="btn1" @click="toManageVoucher(item)">编辑</button>
					<button class="btn2" @click="deleteItem(item)">删除</button>
				</view>
			</view>
		</view>
		<view class="isNull" v-if="list.length==0">
			<image src="@/static/nodata.png" mode=""></image>
			<text>暂时没有数据~</text>
		</view>
		<view class="btn">
			<view class="btn-left">
				共<text>{{list.length}}</text>张
			</view>
			<button class="btn-add" @click="toManageVoucher()">新增</button>
		</view>
	</view>
</template>

<script>
	import {
		getAVoucherListApi
	} from "@/common/api.js"
	export default {
		data() {
			return {
				list: [],
				form: {
					page: 1,
					limit: 10,
				},
				url: '', //图片根路径
			}
		},
		onReachBottom() {
			this.getAVoucherList()
			console.log("触底")
		},
		onShow() {
			this.getAVoucherList(1)
			this.url = this.$base
		},
		methods: {
			// 获取商家端现金券列表
			getAVoucherList(num) {
				if (num) {
					this.form.page = 1
					this.list = []
				} else {
					this.form.page++
				}
				getAVoucherListApi(this.form).then(res => {
					this.list = [...this.list, ...res.list]
				})
			},
			// 去新增/编辑
			toManageVoucher(item) {
				console.log(item)
				uni.navigateTo({
					url: '/pageA/manageVoucher?item=' + JSON.stringify(item)
				})
			},
			// 删除代金券
			deleteItem(item) {
				this.$api.deleteAVoucherApi(item).then(res=>{
					if(res.code === 200001){
						uni.showToast({
							title:'删除成功',
							icon:"success",
							duration:1000
						})
						setTimeout(()=>{
							this.getAVoucherList(1)
						})
					}
				})
				console.log("删除", item)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list {
		margin: 0 30rpx;
		padding-bottom: 200rpx;
	}

	.list-item {
		margin-top: 30rpx;
		background: #fff;
		border-radius: 10rpx;
		padding: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.list-item-left {
		display: flex;
		align-items: center;
	}

	.lil-avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 10rpx;
		flex-shrink: 0;
		margin-right: 20rpx;
	}

	.lil-name {
		margin-top: 10rpx;
		font-size: 28rpx;
	}

	.lil-price {
		font-size: 40rpx;
		color: red;

		text {
			font-size: 26rpx;
		}
	}

	.btn1 {
		width: 120rpx;
		height: 50rpx;
		line-height: 50rpx;
		font-size: 26rpx;
		color: #333;
		background: #fff;
		border: 1rpx solid #e7e7e7;
	}

	.btn2 {
		margin-top: 10rpx;
		width: 120rpx;
		height: 50rpx;
		line-height: 50rpx;
		font-size: 26rpx;
		color: red;
		background: #fff;
		border: 1rpx solid #e7e7e7;
	}

	.btn {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		z-index: 99;
		background: #fff;
		padding: 10rpx 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
	}

	.btn-left {
		font-size: 28rpx;

		text {
			font-size: 36rpx;
			margin: 10rpx;
			color: red;
		}
	}

	.btn-add {
		width: 300rpx;
		height: 80rpx;
		margin: 0;
		border: 1rpx solid #09a0f7;
		line-height: 80rpx;
		background: #09a0f7;
		border-radius: 40rpx;
		font-size: 32rpx;
	}

	.isNull {
		margin-top: 200rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		image {
			width: 410rpx;
			height: 240rpx;
			flex-shrink: 0;
		}

		text {
			margin-top: 30rpx;
			font-size: 28rpx;
			color: #999;
		}
	}
</style>
